package com.lagou.edu.controller;

import com.lagou.edu.pojo.Resume;
import com.lagou.edu.service.IResumeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * @author 13771
 * @date 2020/8/9 22:40
 * @description
 */
@Controller
@RequestMapping("/resume")
public class ResumeController {
    @Autowired
    private IResumeService resumeService;

    @RequestMapping("/findAll")
    public ModelAndView findAll(){
        List<Resume> all = resumeService.findAll();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("data",all);
        modelAndView.setViewName("success");
        return modelAndView;
    }

    @RequestMapping("/save")
    @ResponseBody
    public String save(@RequestBody  Resume resume){
        try {
            resumeService.sava(resume);
            return  "{\"code\":200,\"message\":\"sucess\"}";
        }catch (Exception e){
            e.printStackTrace();
            return e.getMessage();
        }
    }

    @RequestMapping("/update")
    @ResponseBody
    public String update(@RequestBody Resume resume){
        try {
            resumeService.update(resume);
            return  "{\"code\":200,\"message\":\"sucess\"}";
        }catch (Exception e){
            e.printStackTrace();
            return e.getMessage();
        }
    }

    @RequestMapping("/delete/{id}")
    @ResponseBody
    public String delete(@PathVariable Long id){
        try {
            //int i = 1/0;
            resumeService.delete(id);
            return  "{\"code\":200,\"message\":\"sucess\"}";
        }catch (Exception e){
            e.printStackTrace();
            return e.getMessage();
        }
    }
}
